//
//  XQCalendarTool.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/24.
//

#import "XQCalendarTool.h"

@implementation XQCalendarTool

+ (NSArray *)getAllYear:(NSInteger)minYear {
    // 获取当前年份
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear fromDate:now];
    NSInteger currentYear = [components year];
    
    // 创建年份数组
    NSMutableArray *years = [NSMutableArray array];
    
    for (NSInteger year = minYear; year <= currentYear; year++) {
        [years addObject:[NSString stringWithFormat:@"%ld",(long)year]];
    }
    return [years copy]; // 返回不可变数组
}

+ (NSArray *)getAllMonths {
    return @[
        @"January",@"February",@"March",
        @"April",@"May",@"June",
        @"July",@"August",@"September",
        @"October",@"November",@"December"];
}

+ (NSString *)getMonthWordWithIndex:(NSInteger)index {
    return [self getAllMonths][index - 1];
}

+ (NSArray *)getDaysWithYear:(NSInteger)year month:(NSInteger)month {
    // 创建一个日历对象
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    // 创建日期组件
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.year = year;
    components.month = month;
    
    // 获取该月的第一天
    components.day = 1;
    NSDate *firstDayOfMonth = [calendar dateFromComponents:components];
    
    // 获取该月的范围
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:firstDayOfMonth];
    
    // 创建一个数组来存储天数
    NSMutableArray *daysArray = [NSMutableArray array];
    
    // 将每一天添加到数组中
    for (NSInteger day = 1; day <= range.length; day++) {
        [daysArray addObject:[NSString stringWithFormat:@"%ld", (long)day]];
    }
    
    return [daysArray copy]; // 返回不可变数组
}

+ (NSInteger)getMonthIndexWithValue:(NSString *)value {
    NSArray *months = [self getAllMonths];
    NSInteger index = -1;
    for (NSString *month in months) {
        if ([value isEqualToString:month]) {
            index = [months indexOfObject:month];
            break;
        }
    }
    return index + 1;
}

@end
